home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 1 Issue 2 / PDCD-1 - Issue 02.iso / _utilities / utilities / 003 / _gs / !GS / c / ZGSTATE < prev    next >
Text File  |  1991-10-25  |  6KB  |  271 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zgstate.c */
  21. /* Graphics state operators for Ghostscript */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "alloc.h"
  26. #include "gsmatrix.h"
  27. #include "gsstate.h"
  28. #include "state.h"
  29. #include "store.h"
  30.  
  31. /* Forward references */
  32. private int num_param(P2(os_ptr, int (*)(P2(gs_state *, floatp))));
  33. private int line_param(P2(os_ptr, int *));
  34.  
  35. /* The current graphics state */
  36. gs_state *igs;
  37. int_state istate;
  38.  
  39. /* Initialize the graphics stack. */
  40. void
  41. gs_init()
  42. {    igs = gs_state_alloc(alloc, alloc_free);
  43.     istate.saved = 0;
  44.     make_null(&istate.screen_proc);
  45.     make_null(&istate.transfer_procs.red);
  46.     make_null(&istate.transfer_procs.green);
  47.     make_null(&istate.transfer_procs.blue);
  48.     make_null(&istate.transfer_procs.gray);
  49.     /* gsave and grestore only work properly */
  50.     /* if there are always at least 2 entries on the stack. */
  51.     /* We count on the PostScript initialization code to do a gsave. */
  52. }
  53.  
  54. /* gsave */
  55. int
  56. zgsave(register os_ptr op)
  57. {    int code = gs_gsave(igs);
  58.     int_state *pis = (int_state *)alloc(1, sizeof(int_state), "gsave");
  59.     if ( pis == 0 ) code = e_VMerror;
  60.     if ( code < 0 ) return e_limitcheck;
  61.     *pis = istate;
  62.     istate.saved = pis;
  63.     return 0;
  64. }
  65.  
  66. /* grestore */
  67. int
  68. zgrestore(register os_ptr op)
  69. {    int code = gs_grestore(igs);
  70.     int_state *pis;
  71.     if ( code < 0 ) return code;
  72.     pis = istate.saved;
  73.     istate = *pis;
  74.     if ( istate.saved )
  75.         alloc_free((char *)pis, 1, sizeof(int_state), "grestore");
  76.     else                /* bottom of stack */
  77.         istate.saved = pis;
  78.     return 0;
  79. }
  80.  
  81. /* grestoreall */
  82. int
  83. zgrestoreall(register os_ptr op)
  84. {    while ( istate.saved->saved )
  85.        {    int code = zgrestore(op);
  86.         if ( code < 0 ) return code;
  87.        }
  88.     return zgrestore(op);
  89. }
  90.  
  91. /* initgraphics */
  92. int
  93. zinitgraphics(register os_ptr op)
  94. {    return gs_initgraphics(igs);
  95. }
  96.  
  97. /* setlinewidth */
  98. int
  99. zsetlinewidth(register os_ptr op)
  100. {    return num_param(op, gs_setlinewidth);
  101. }
  102.  
  103. /* currentlinewidth */
  104. int
  105. zcurrentlinewidth(register os_ptr op)
  106. {    push(1);
  107.     make_real(op, gs_currentlinewidth(igs));
  108.     return 0;
  109. }
  110.  
  111. /* setlinecap */
  112. int
  113. zsetlinecap(register os_ptr op)
  114. {    int param;
  115.     int code = line_param(op, ¶m);
  116.     if ( !code ) code = gs_setlinecap(igs, (gs_line_cap)param);
  117.     return code;
  118. }
  119.  
  120. /* currentlinecap */
  121. int
  122. zcurrentlinecap(register os_ptr op)
  123. {    push(1);
  124.     make_int(op, (int)gs_currentlinecap(igs));
  125.     return 0;
  126. }
  127.  
  128. /* setlinejoin */
  129. int
  130. zsetlinejoin(register os_ptr op)
  131. {    int param;
  132.     int code = line_param(op, ¶m);
  133.     if ( !code ) code = gs_setlinejoin(igs, (gs_line_join)param);
  134.     return code;
  135. }
  136.  
  137. /* currentlinejoin */
  138. int
  139. zcurrentlinejoin(register os_ptr op)
  140. {    push(1);
  141.     make_int(op, (int)gs_currentlinejoin(igs));
  142.     return 0;
  143. }
  144.  
  145. /* setmiterlimit */
  146. int
  147. zsetmiterlimit(register os_ptr op)
  148. {    return num_param(op, gs_setmiterlimit);
  149. }
  150.  
  151. /* currentmiterlimit */
  152. int
  153. zcurrentmiterlimit(register os_ptr op)
  154. {    push(1);
  155.     make_real(op, gs_currentmiterlimit(igs));
  156.     return 0;
  157. }
  158.  
  159. /* setdash */
  160. int
  161. zsetdash(register os_ptr op)
  162. {    float offset;
  163.     uint n, i;
  164.     ref *dfrom;
  165.     float *pattern, *dto;
  166.     int code = real_param(op, &offset);
  167.     if ( code ) return code;
  168.     check_array(op[-1]);
  169.     check_read(op[-1]);
  170.     /* Unpack the dash pattern and check it */
  171.     dfrom = op[-1].value.refs;
  172.     i = n = r_size(op - 1);
  173.     pattern = dto = (float *)alloc(n, sizeof(float), "setdash");
  174.     while ( i-- )
  175.        {    switch ( r_type(dfrom) )
  176.            {
  177.         case t_integer:
  178.             *dto++ = dfrom->value.intval;
  179.             break;
  180.         case t_real:
  181.             *dto++ = dfrom->value.realval;
  182.             break;
  183.         default:
  184.             alloc_free((char *)dto, n, sizeof(float), "setdash");
  185.             return e_typecheck;
  186.            }
  187.         dfrom++;
  188.        }
  189.     code = gs_setdash(igs, pattern, n, offset);
  190.     if ( !code ) pop(2);
  191.     return code;
  192. }
  193.  
  194. /* currentdash */
  195. int
  196. zcurrentdash(register os_ptr op)
  197. {    int n = gs_currentdash_length(igs);
  198.     int i = n;
  199.     ref *pattern = alloc_refs(n, "currentdash");
  200.     ref *dto = pattern;
  201.     float *dfrom = (float *)((char *)pattern + n * (sizeof(ref) - sizeof(float)));
  202.     gs_currentdash_pattern(igs, dfrom);
  203.     while ( i-- )
  204.        {    make_real(dto, *dfrom);
  205.         dto++, dfrom++;
  206.        }
  207.     push(2);
  208.     make_tasv(op - 1, t_array, a_all, n, refs, pattern);
  209.     make_real(op, gs_currentdash_offset(igs));
  210.     return 0;
  211. }
  212.  
  213. /* setflat */
  214. int
  215. zsetflat(register os_ptr op)
  216. {    return num_param(op, gs_setflat);
  217. }
  218.  
  219. /* currentflat */
  220. int
  221. zcurrentflat(register os_ptr op)
  222. {    push(1);
  223.     make_real(op, gs_currentflat(igs));
  224.     return 0;
  225. }
  226.  
  227. /* ------ Initialization procedure ------ */
  228.  
  229. op_def zgstate_op_defs[] = {
  230.     {"0currentdash", zcurrentdash},
  231.     {"0currentflat", zcurrentflat},
  232.     {"0currentlinecap", zcurrentlinecap},
  233.     {"0currentlinejoin", zcurrentlinejoin},
  234.     {"0currentlinewidth", zcurrentlinewidth},
  235.     {"0currentmiterlimit", zcurrentmiterlimit},
  236.     {"0grestore", zgrestore},
  237.     {"0grestoreall", zgrestoreall},
  238.     {"0gsave", zgsave},
  239.     {"0initgraphics", zinitgraphics},
  240.     {"2setdash", zsetdash},
  241.     {"1setflat", zsetflat},
  242.     {"1setlinecap", zsetlinecap},
  243.     {"1setlinejoin", zsetlinejoin},
  244.     {"1setlinewidth", zsetlinewidth},
  245.     {"1setmiterlimit", zsetmiterlimit},
  246.     op_def_end(0)
  247. };
  248.  
  249. /* ------ Internal routines ------ */
  250.  
  251. /* Get a numeric parameter */
  252. private int
  253. num_param(os_ptr op, int (*pproc)(P2(gs_state *, floatp)))
  254. {    float param;
  255.     int code = real_param(op, ¶m);
  256.     if ( !code ) code = (*pproc)(igs, param);
  257.     if ( !code ) pop(1);
  258.     return code;
  259. }
  260.  
  261. /* Get an integer parameter 0-2. */
  262. private int
  263. line_param(register os_ptr op, int *pparam)
  264. {    check_type(*op, t_integer);
  265.     if ( op->value.intval < 0 || op->value.intval > 2 )
  266.         return e_rangecheck;
  267.     *pparam = (int)op->value.intval;
  268.     pop(1);
  269.     return 0;
  270. }
  271.